package com.cj.allTest.testJson;

import com.cj.DesignMode.dataAccessObject.Student;
import com.google.common.collect.Sets;
import org.springframework.util.CollectionUtils;

import java.text.DecimalFormat;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author chan
 * @dateTime 2023/3/6  19:00:53
 */
public class Test {

    @org.junit.Test
    public void main() {
        List<Student> stus = new ArrayList<>();
        stus.add(new Student("cj,", 1001));
        stus.add(new Student("xb,", 1001));
        stus.add(new Student("xc,", 1001));
        stus.add(new Student("xd,", 1001));
        stus.add(new Student("xa,", 1001));
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        Map<Integer, String> collect = stus.stream()
                .filter(Objects::nonNull)
                .filter(filterParam(Student::getRollNo))
                .collect(Collectors.groupingBy(Student::getRollNo, Collectors.mapping(Student::getName, Collectors.joining(",", "Post titles: [", "]"))));
        System.out.println(collect);
    }

    public <T> Predicate<T> filterParam(Function<? super T, ?> function) {
        Set<Object> all = new HashSet<>();
        Predicate<T> tPredicate = t -> all.add(function.apply(t));
        return tPredicate;
    }

    public static Set<Integer> getNotIn(List<Integer> list1, List<Integer> list2) {

        if (CollectionUtils.isEmpty(list2)) {
            return Sets.newConcurrentHashSet(list1);
        }

        return list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toSet());
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i >= 0; i++) {
            String run = new Test().run();
            System.out.println(run);
        }
    }

    public String run() throws InterruptedException {
        Random random = new Random();
        double v = random.nextDouble();
        DecimalFormat df = new DecimalFormat("#.00000");
        Thread.sleep(5000L);
        System.out.println(v);
        return df.format(v);
    }
}
